package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bean.UserPO;
import com.example.bean.SearchCodePO;
import com.example.dto.SaveSqlSettingDTO;
import com.example.mapper.mysql.mysqlMapper;
import com.example.mapper.starrocks.StarRocksMapper;
import com.example.service.DataService;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.example.config.constant.ConstantConfig.REDIS_SQL_CODE_KEY_PREFIX;

@Service
public class DataServiceImpl implements DataService {


    mysqlMapper mysqlMapper;
    StarRocksMapper starRocksMapper;
    RedissonClient redissonClient;


    @Autowired
    public DataServiceImpl(mysqlMapper mysqlMapper, StarRocksMapper starRocksMapper, RedissonClient redissonClient) {
        this.mysqlMapper = mysqlMapper;
        this.starRocksMapper = starRocksMapper;
        this.redissonClient = redissonClient;
    }

/*    @Override
    public String addOneData(String name) {
        DataBean dataBean = new DataBean();
        dataBean.setData(name);
        int insert = oneMapper.insert(dataBean);
        return insert>0?"插入成功":"插入失败";
    }*/

    @Override
    public List<SearchCodePO> getMysqlData() {
        return mysqlMapper.selectList(new QueryWrapper<SearchCodePO>());
    }

    @Override
    public List<UserPO> getStarRocksData() {
        return starRocksMapper.selectList(new QueryWrapper<UserPO>());
    }


    @Override
    public Boolean saveSqlSetting(SaveSqlSettingDTO saveSqlSettingDTO) {
        SearchCodePO searchCodePO = selectSqlByCode(saveSqlSettingDTO.getSqlCode());
        if (searchCodePO == null) {
            SearchCodePO insertPO = new SearchCodePO();
            insertPO.setSqlCode(saveSqlSettingDTO.getSqlCode());
            insertPO.setSqlContent(saveSqlSettingDTO.getSqlContent());
            mysqlMapper.insert(insertPO);
        } else {
            SearchCodePO updatePO = new SearchCodePO();
            updatePO.setId(searchCodePO.getId());
            updatePO.setSqlContent(saveSqlSettingDTO.getSqlContent());
            mysqlMapper.updateById(updatePO);
        }
        RBucket<String> bucket = redissonClient.getBucket(REDIS_SQL_CODE_KEY_PREFIX + saveSqlSettingDTO.getSqlCode());
        if (bucket != null) {
            bucket.delete();
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean removeCatch(List<String> sqlCodeList) {
        sqlCodeList.parallelStream().forEach(sqlCode -> {
            RBucket<Object> bucket = redissonClient.getBucket(REDIS_SQL_CODE_KEY_PREFIX + sqlCode);
            if (bucket != null) {
                bucket.delete();
            }
        });
        return Boolean.TRUE;
    }

    @Override
    public SearchCodePO selectSqlByCode(String sqlCode) {
        LambdaQueryWrapper<SearchCodePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SearchCodePO::getSqlCode, sqlCode);
        return mysqlMapper.selectOne(queryWrapper);
    }

    @Override
    public String selectSql(String sqlCode) {
        RBucket<String> bucket = redissonClient.getBucket(REDIS_SQL_CODE_KEY_PREFIX + sqlCode);
        if (bucket != null && bucket.get() != null) {
            return bucket.get();
        }
        String sqlContent = mysqlMapper.selectSql(sqlCode);
        if (bucket != null && sqlContent != null) {
            bucket.set(sqlContent);
        }
        return sqlContent;
    }

    @Override
    public String selectSqlAndSource(String sqlCode) {
        RBucket<String> bucket = redissonClient.getBucket(REDIS_SQL_CODE_KEY_PREFIX + sqlCode);
        if (bucket != null && bucket.get() != null) {
            return "FROM_REDIS: \n" + bucket.get();
        }
        String sqlContent = mysqlMapper.selectSql(sqlCode);
        return "FROM_MYSQL: \n" + sqlContent;
    }

    @Override
    public Page<Map<String, Object>> searchData(Page page, Object param) {
        return starRocksMapper.searchData(page, param);
    }
}
